package com.hck.trigger.http;

import com.hck.types.common.AliOssUtil;
import com.hck.types.model.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * CommonController class description here.
 *
 * @author 黄朝看
 * @date 2025/7/4
 */
@Slf4j
@RestController
@RequestMapping("/api/common")
@Tag(name = "通用接口", description = "头像上传等操作")
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "上传文件")
    public Response<String> upload(MultipartFile file){
        log.info("上传文件,{}",file);

        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成新的文件名
            String objectName = UUID.randomUUID().toString() + extension;
            //文件请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Response.<String>builder()
                    .data(filePath)
                    .info("上传成功")
                    .code("200")
                    .build();
        } catch (IOException e) {
            log.error("上传文件异常: {}",e);
        }
        return Response.<String>builder()
                .data(null)
                .info("上传失败")
                .code("400")
                .build();
    }

}  
